package com.youjia.service;

import com.youjia.mapper.ProductOrderMapper;
import com.youjia.pojo.ProductOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Override
    @Transactional
    public void createorderList(ProductOrder productOrder) {
        productOrderMapper.insertOrderItem(productOrder);
    }

    @Override
    public List<ProductOrder> getOrderItem(Integer userId) {
        /*p.product_id,p.product_name,p.num,p.price
        * o.order_id,o.created,o.payment
        * p.product_id,p.product_name,p.num,p.price
        * */
        List<ProductOrder> productOrderList = new ArrayList<>();
        List<ProductOrder> order = productOrderMapper.getOrder(userId);
        System.out.println(order);
        for (ProductOrder proOrder : order) {
            ProductOrder productOrder = new ProductOrder();
            List<ProductOrder> orderItem = productOrderMapper.getOrderItem(proOrder.getOrderId());
            productOrder.setOrderId(proOrder.getOrderId()).setPayment(proOrder.getPayment())
                    .setProductList(orderItem).setIspayment(proOrder.getIspayment())
                    .setCreated(proOrder.getCreated());
            productOrderList.add(productOrder);
        }
        System.out.println(productOrderList);
        return productOrderList;

    }


    @Override
    public void insertOrder(ProductOrder productOrder) {
        productOrder.setIspayment(0);
        productOrder.setOrderAdd("山东省济南市");
        productOrder.setStatus(1);
        productOrderMapper.createorderList(productOrder);
        /*productOrderMapper.deleteShopping(productOrder);*/
    }

    @Override
    public void updateOrder() {
        Integer orderId = productOrderMapper.getOrderId();
        ProductOrder productOrder = new ProductOrder();
        productOrder.setOrderId(orderId);
        productOrder.setIspayment(1);
        productOrderMapper.updateIsPayMent(productOrder);
    }
}
